package net.minecraft.src;

import java.util.Random;

public class BlockTimeBox extends Block
{

	protected BlockTimeBox(int i, int j)
	{
		super(i, j, Material.rock);
	}

	// Right Click!
	public boolean blockActivated(World world, int x, int y, int z, EntityPlayer player)
	{
		if (isOpen(world, x, y, z))
		{
			destroyPortal(world, x, y, z);
			Close(world, x, y, z);
			dropBlockAsItem_do(world, x, y + 1, z, new ItemStack(mod_pph.timeRod));
		}
		else
		{
			ItemStack currentItem = player.getCurrentEquippedItem();
			if (currentItem == null)
				return false;
			if (currentItem.itemID == mod_pph.timeRod.shiftedIndex)
			{
				if (createPortal(world, x, y, z))
				{
					player.getCurrentEquippedItem().damageItem(10, null);
					Open(world, x, y, z);
				}
			}
		}
		return true;
	}

	private boolean createPortal(World world, int x, int y, int z)
	{
		y += 1;
		if (isAirLine(world, x - 2, y, z, 5) && isAirLine(world, x - 2, y, z, 5) && isAirLine(world, x - 2, y, z, 5) &&
			isAirLine(world, x - 1, y + 1, z, 3) && isAirLine(world, x - 1, y + 1, z, 3) &&	isAirLine(world, x - 1, y + 1, z, 3) &&
			isAirLine(world, x - 1, y + 2, z, 3) && isAirLine(world, x - 1, y + 2, z, 3) && isAirLine(world, x - 1, y + 2, z, 3)
			)
		{
			formPortal(world, x, y, z, 44, 109, 98, mod_pph.portalBlock.blockID);
			return true;
		}
		return false;
	}

	private void formPortal(World world, int x, int y, int z, int halfStep, int stair, int block, int portal)
	{
		//0 - West
		//1 - East
		//2 - South
		//3 - North
		//Layer 1
		world.editingBlocks = true;
		world.setBlockAndMetadata(x - 2, y, z + 2, halfStep, 5);
		world.setBlockAndMetadata(x - 1, y, z + 2, stair, 3);
		world.setBlock(			  x,     y, z + 2, 0);
		world.setBlockAndMetadata(x + 1, y, z + 2, stair, 3);
		world.setBlockAndMetadata(x + 2, y, z + 2, halfStep, 5);
		
		world.setBlockAndMetadata(x - 2, y, z + 1, stair, 0);
		world.setBlock(           x - 1, y, z + 1, block);
		world.setBlock(           x,     y, z + 1, 0);
		world.setBlock(           x + 1, y, z + 1, block);
		world.setBlockAndMetadata(x + 2, y, z + 1, stair, 1);
		
		world.setBlock(			  x - 2, y, z, 0);
		world.setBlock(           x - 1, y, z, 0);
		world.setBlock(           x,     y, z, portal);
		world.setBlock(           x + 1, y, z, 0);
		world.setBlock(			  x + 2, y, z, 0);
		
		world.setBlockAndMetadata(x - 2, y, z - 1, stair, 0);
		world.setBlock(           x - 1, y, z - 1, block);
		world.setBlock(           x,     y, z - 1, 0);
		world.setBlock(           x + 1, y, z - 1, block);
		world.setBlockAndMetadata(x + 2, y, z - 1, stair, 1);
		
		world.setBlockAndMetadata(x - 2, y, z - 2, halfStep, 5);
		world.setBlockAndMetadata(x - 1, y, z - 2, stair, 2);
		world.setBlockAndMetadata(x,     y, z - 2, 0, 2);
		world.setBlockAndMetadata(x + 1, y, z - 2, stair, 2);
		world.setBlockAndMetadata(x + 2, y, z - 2, halfStep, 5);
		
		y += 1;
		//Layer 2
		world.setBlock( 		  x - 1, y, z + 1, block);
		world.setBlock(		      x,     y, z + 1, 0);
		world.setBlock(			  x + 1, y, z + 1, block);
		
		world.setBlock(			  x - 1, y, z    , 0);
		world.setBlock(           x    , y, z    , portal);
		world.setBlock(			  x + 1, y, z    , 0);
		
		world.setBlock(			  x - 1, y, z - 1, block);
		world.setBlock(			  x,     y, z - 1, 0);
		world.setBlock(			  x + 1, y, z - 1, block);
		 
		y += 1;
		//Layer 3
		world.setBlockAndMetadata(x - 1, y, z + 1, halfStep, 5);
		world.setBlockAndMetadata(x,     y, z + 1, halfStep, 5);
		world.setBlockAndMetadata(x + 1, y, z + 1, halfStep, 5);
		
		world.setBlockAndMetadata(x - 1, y, z    , halfStep, 5);
		world.setBlock(			  x    , y, z    , block);
		world.setBlockAndMetadata(x + 1, y, z    , halfStep, 5);
		
		world.setBlockAndMetadata(x - 1, y, z - 1, halfStep, 5);
		world.setBlockAndMetadata(x,     y, z - 1, halfStep, 5);
		world.setBlockAndMetadata(x + 1, y, z - 1, halfStep, 5);
		
		world.editingBlocks = false;
	}

	private void destroyPortal(World world, int x, int y, int z)
	{
		if (!isOpen(world, x, y, z))
			return;
		formPortal(world, x, y + 1, z, 0, 0, 0, 0);
	}

	private void fillLine(World world, int x, int y, int z, int width, int blockId)
	{
		for (int i = x; i < (x + width); i++)
		{
			world.setBlockWithNotify(i, y, z, blockId);
		}
	}

	private boolean isAirLine(World world, int x, int y, int z, int width)
	{
		for (int i = x; i < (x + width); i++)
		{
			if (world.getBlockId(i, y, z) != 0)
			{
				return false;
			}
		}
		return true;
	}

	public void onBlockRemoval(World world, int x, int y, int z)
	{
		destroyPortal(world, x, y, z);
		if(isOpen(world, x, y, z))
		{
			Close(world, x, y, z);
			dropBlockAsItem_do(world, x, y + 1, z, new ItemStack(mod_pph.timeRod));
		}
	}

	public int quantityDropped(Random random)
	{
		return 1;
	}

	public int idDropped(int i, Random random)
	{
		return mod_pph.timeBox.blockID;
	}

	public boolean isOpen(World world, int x, int y, int z)
	{
		return world.getBlockMetadata(x, y, z) == 1;
	}

	public void Close(World world, int x, int y, int z)
	{
		world.setBlockMetadata(x, y, z, 0);
	}

	public void Open(World world, int x, int y, int z)
	{
		world.setBlockMetadata(x, y, z, 1);
	}

}
